const wordbooks = [
  {
    id: 'ielts-essential',
    name: '雅思词汇胜经',
    description: '收录雅思考试必备词汇，按照主题和词频分类整理',
    totalWords: 4000,
    cover: '/images/books/ielts-essential.png',
    progress: 0
  },
  {
    id: 'ielts-core-7days',
    name: '7天搞定雅思高频核心词',
    description: '精选雅思考试中最常见的核心词汇，7天速记版本',
    totalWords: 1000,
    cover: '/images/books/ielts-core-7days.png',
    progress: 0
  },
  {
    id: 'ielts-shuffle',
    name: '雅思词汇乱序版',
    description: '打乱顺序的雅思词汇，帮助巩固记忆',
    totalWords: 3000,
    cover: '/images/books/ielts-shuffle.png',
    progress: 0
  },
  {
    id: 'ielts-vocabulary',
    name: 'IELTS词汇',
    description: '完整的雅思考试词汇列表',
    totalWords: 5000,
    cover: '/images/books/ielts-vocabulary.png',
    progress: 0
  },
  {
    id: 'ielts-core',
    name: '雅思核心词汇',
    description: '雅思考试中最重要的核心词汇',
    totalWords: 2000,
    cover: '/images/books/ielts-core.png',
    progress: 0
  }
]

// 获取所有词库列表
const getWordbooks = () => {
  return wordbooks
}

// 获取指定词库信息
const getWordbook = (id) => {
  return wordbooks.find(book => book.id === id)
}

// 获取词库中的单词列表
const getWords = async (bookId, unitId = 1) => {
  try {
    const words = await wx.getStorage({
      key: `wordbook_${bookId}_unit_${unitId}`
    })
    return words.data
  } catch (error) {
    // 如果本地没有缓存，则从文件加载
    const words = require(`../data/wordbooks/${bookId}.json`)
    const unit = words.units.find(u => u.unit_id === unitId)
    
    // 缓存到本地
    wx.setStorage({
      key: `wordbook_${bookId}_unit_${unitId}`,
      data: unit.words
    })
    
    return unit.words
  }
}

// 更新学习进度
const updateProgress = (bookId, progress) => {
  const book = wordbooks.find(b => b.id === bookId)
  if (book) {
    book.progress = progress
    // 保存到本地存储
    wx.setStorage({
      key: `progress_${bookId}`,
      data: progress
    })
  }
}

// 获取学习进度
const getProgress = async (bookId) => {
  try {
    const res = await wx.getStorage({
      key: `progress_${bookId}`
    })
    return res.data
  } catch (error) {
    return 0
  }
}

module.exports = {
  getWordbooks,
  getWordbook,
  getWords,
  updateProgress,
  getProgress
} 